package web.init;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DB.DB_connection_pool;
import bean.Sports_record;
import bean.Sports_recordList;

public class Sports_recordList_init extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		
		Timer timer = new Timer();
		timer.schedule(new TimerTask(){public void run(){
			
			List<Sports_record>  sports_records = new ArrayList<Sports_record>();
			
			
			String select = "select * from user_data";
			
			try {
				Connection conn = DB_connection_pool.data_pool.getConnection();
				PreparedStatement ps_select= conn.prepareStatement(select);
				ResultSet rs = ps_select.executeQuery();
				
				while(rs.next()){
					Sports_record sports_record = new Sports_record();
					
					sports_record.setUser_account(rs.getString("user_account"));
					sports_record.setBasketball_record(rs.getDouble("basketball_record"));
					sports_record.setRunning_record(rs.getDouble("running_record"));
					sports_record.setSwimming_record(rs.getDouble("swimming_record"));
					sports_record.setUser_level(rs.getInt("user_level"));
					sports_record.setUser_nickname(rs.getString("user_nickname"));
					sports_record.setUser_photo_path(rs.getString("user_photo_path"));
					sports_record.setUser_introduction(rs.getString("user_introduction"));
					
					sports_records.add(sports_record);
				}
				Sports_recordList.setSports_records(sports_records);
				
				rs.close();
				ps_select.close();
				conn.close();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}}, 0,1000*60*60*24);   //每24小时更新一次排行榜
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
